package cxm.mmp.aud;

import de.tavendo.autobahn.WebSocketConnection;

public class WebSocketRecordingSave implements IRecordingSource {
    WebSocketConnection connection = null;
	public WebSocketRecordingSave(WebSocketConnection connect) {
		if (null == connect)
			throw new NullPointerException("connect");
		
		this.connection = connect;
	}

    // do before data transfer, success to return 0
 	public int onRecordStart() {
 		// remain empty for no need to disconnect
 		return 0;
 	}
 	
 	// do manipulate recording data
 	// return data processed, less than 0 for error
 	public int onRecordData(byte []data, int start, int num) {
 		// send data to web socket
 		byte []dataSend = null;
 		if (start == 0 && num == data.length)
 			dataSend = data;
 		else {
 			// copy memory if necessary
	 		dataSend = new byte[num];
	 		for (int i = 0; i < num; i++)
	 			dataSend[i] = data[start + i];
 		}

 		this.connection.sendBinaryMessage(dataSend);
 		return num;
 	}
 	
 	// do after data transfer, success to return 0
 	public int onRecordStop() {
 		// remain empty for no need to disconnect
 		return 0;
 	}
 	
 	public int onRecordError(RecordError error, int errno) {
 		return 0;
 	}
}
